package com.zqweb.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zqweb.common.exception.ExpCodeAndMsgEnum;
import com.zqweb.common.vo.GiteeSocialUserInfo;
import com.zqweb.common.vo.GiteeUserInfo;
import com.zqweb.gulimall.member.exception.PhoneException;
import com.zqweb.gulimall.member.exception.UserNameException;
import com.zqweb.gulimall.member.feign.FeignTest;
import com.zqweb.gulimall.member.vo.UserLoginVo;
import com.zqweb.gulimall.member.vo.UserRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import com.zqweb.gulimall.member.entity.MemberEntity;
import com.zqweb.gulimall.member.service.MemberService;
import com.zqweb.common.utils.PageUtils;
import com.zqweb.common.utils.R;


/**
 * 会员
 *
 * @author zq
 * @email zq@gmail.com
 * @date 2021-11-26 20:06:31
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private FeignTest feignTest;


    /**
     * 根据传入的指定用户信息，进行注册存储
     */
    @PostMapping("/register")
    public R registerUser(@RequestBody UserRegistVo userRegistVo){
        try {
            memberService.registerUser(userRegistVo);
            // 根据抛出的不同类型的异常，采取不同的异常应对策略
        } catch (UserNameException e) {
            System.out.println(e.getMessage());
            return R.error(ExpCodeAndMsgEnum.USER_EXIST_EXCEPTION.getCode(),ExpCodeAndMsgEnum.USER_EXIST_EXCEPTION.getMsg());
        } catch (PhoneException e) {
            System.out.println(e.getMessage());
            return R.error(ExpCodeAndMsgEnum.PHONE_EXIST_EXCEPTION.getCode(),ExpCodeAndMsgEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }
    // 非第三方用户登陆
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo){
        MemberEntity memberEntity = memberService.login(userLoginVo);
        if(memberEntity != null){
            return R.ok().put("memberUserInfo",memberEntity);
        }else{
            System.out.println("返回的异常信息为：" + ExpCodeAndMsgEnum.USERACCOUNT_PASSWORD_EXCEPTION.getMsg());
            return R.error(ExpCodeAndMsgEnum.USERACCOUNT_PASSWORD_EXCEPTION.getCode(),ExpCodeAndMsgEnum.USERACCOUNT_PASSWORD_EXCEPTION.getMsg());
        }
    }
    // 第三方用户登陆
    @PostMapping("/login/thirdparty")
    public R loginByThirdparty(@RequestBody GiteeSocialUserInfo giteeSocialUserInfo){
        MemberEntity memberEntity = memberService.loginByThirdparty(giteeSocialUserInfo);
        return R.ok().put("memberUserInfo",memberEntity);
    }

    /**
     * 测试feign的远程服务调用
     */
    @RequestMapping("/feignTest")
    public R feignTest(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        // 通过feignTest接口，远程调用特定的服务
        R r = feignTest.testFeign();

        return R.ok().put("member",memberEntity).put("coupon",r.get("coupon"));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
